// ##############################
// ####  note:  bokeweilai common main fun
// ####  create:  TANWENHUI (twh:谭文辉)
// ####  date:   2024/6/18
// ##############################

#include "bkwl_app.h"
#include "bkwl_data.h"
#include "bkwl_network.h"
#include "dev_log.h"
#include "bkwl_event.h"
#include "bkwl_protocol_jiwei.h"
#include "bkwl_app.h"

#if 1
bkwl_app_t *bkwl_app;
beken_thread_t thread_uart_msg;
beken_queue_t app_queue_uart_door;
bkwl_err_t bkwl_app_uart_door_handle(void)
{
	bkwl_err_t ret = BK_OK;
	bkwl_msg_t msg;
	bprintf("bkwl_app_uart_door_handle  entry\n");
	while (1)
	{

		if (app_queue_uart_door != NULL)
		{
			ret = rtos_pop_from_queue(&app_queue_uart_door, &msg, BEKEN_WAIT_FOREVER);
			if (kNoErr == ret)
			{
				switch (msg.cmd)
				{
				case BKWL_CMD_DOOR_START_WIFI_PAIR:
				{
					bkwl_door_wifi_pair_t *bkwl_door_wifi_pair = (bkwl_door_wifi_pair_t *)msg.data;
					//  bkwl_door_wifi_pair->type = data[5];
					//  bkwl_door_wifi_pair->notify = data[7];
					bprintf("app main get BKWL_CMD_START_WIFI_PAIR \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_PAIR_FAIL, msg.param);
					free(bkwl_door_wifi_pair);
					break;
				}
				case BKWL_CMD_DOOR_MESSAGE:
				{
					bkwl_door_message_t *bkwl_door_message = (bkwl_door_message_t *)msg.data;
					bprintf("app main get BKWL_CMD_DOOR_MESSAGE \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					free(bkwl_door_message);
					break;
				}
				case BKWL_CMD_DOOR_POWER_OFF:
				{
					bkwl_door_power_off_t *bkwl_door_power_off = (bkwl_door_power_off_t *)msg.data;
					bprintf("app main get BKWL_CMD_DOOR_POWER_OFF \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					free(bkwl_door_power_off);
					break;
				}
				case BKWL_CMD_DOOR_CLEAN_DATA:
				{
					bprintf("app main get BKWL_CMD_DOOR_CLEAN_DATA \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_GET_DEVICE_INFO:
				{
					bprintf("app main get BKWL_CMD_DOOR_GET_DEVICE_INFO \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_UPGRADE:
				{
					bprintf("app main get BKWL_CMD_DOOR_UPGRADE \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_PRODUCT_TEST:
				{
					bprintf("app main get BKWL_CMD_DOOR_PRODUCT_TEST \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_GET_NET_TIME:
				{
					bprintf("app main get BKWL_CMD_DOOR_GET_NET_TIME \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_ACK_REMOTE_UNLOCK:
				{
					bprintf("app main get BKWL_CMD_DOOR_ACK_REMOTE_UNLOCK \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_UPLOAD_IMAGE:
				{
					bprintf("app main get BKWL_CMD_DOOR_UPLOAD_IMAGE \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_USER_VERIFY:
				{
					bprintf("app main get BKWL_CMD_DOOR_USER_VERIFY \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_START_UPLOAD_IMAGE:
				{
					bprintf("app main get BKWL_CMD_DOOR_START_UPLOAD_IMAGE \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_VIDEO_STATUS_NOTIFY:
				{
					bprintf("app main get BKWL_CMD_DOOR_VIDEO_STATUS_NOTIFY \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_VIDEO_PARA_SETUP:
				{
					bprintf("app main get BKWL_CMD_DOOR_VIDEO_PARA_SETUP \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_STATUS_REPORT:
				{
					bprintf("app main get BKWL_CMD_DOOR_STATUS_REPORT \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_STOP_WIFI_PAIR:
				{
					bprintf("app main get BKWL_CMD_DOOR_STOP_WIFI_PAIR \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_CACHE_MESSAGE:
				{
					bprintf("app main get BKWL_CMD_DOOR_CACHE_MESSAGE \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_DEVICE_FUNC:
				{
					bprintf("app main get BKWL_CMD_DOOR_DEVICE_FUNC \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_LOOK_SETUP:
				{
					bprintf("app main get BKWL_CMD_DOOR_LOOK_SETUP \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_GET_VERSION:
				{
					bprintf("app main get BKWL_CMD_DOOR_GET_VERSION \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_GET_WEATHER:
				{
					bprintf("app main get BKWL_CMD_DOOR_GET_WEATHER \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				case BKWL_CMD_DOOR_GET_REGION:
				{
					bprintf("app main get BKWL_CMD_DOOR_GET_REGION \n");
					bkwl_protocol_jiwei_door_ack(BKWL_JIWEI_CMD_WIFI_ACK, BKWL_JIWEI_CMD_ACK_OK, msg.param);
					break;
				}
				}
			}
		}
		else
		{
			rtos_delay_milliseconds(1000);
		}
	}
	return ret;
}

bkwl_err_t bkwl_app_main()
{
	bk_err_t ret = BK_OK;
	uint32_t num = 0;
	bprintf("bkwl_app_main entry\n\n");
	bkwl_app = (bkwl_app_t *)os_malloc(sizeof(bkwl_app_t));

	// // 1. twh init log
	// ret = bkwl_log_init();
	// // 2. twh init data
	// ret = bkwl_data_init();
	// // 3. twh init gpio
	// // 4. twh init net
	// ret = bkwl_network_init();
	// // 5. twh init device
	// // 6. twh init event
	// ret = bkwl_event_init();
	ret = rtos_init_queue(&app_queue_uart_door,
						  "queue.app_uart_door",
						  sizeof(bkwl_msg_t),
						  10);
	if (ret != BK_OK)
	{
		bprintf("%s, ceate blwl uart_door queue failed\n");
	}
	ret = rtos_create_thread(&thread_uart_msg,
							 BEKEN_DEFAULT_WORKER_PRIORITY,
							 "task.app_uart_msg",
							 (beken_thread_function_t)bkwl_app_uart_door_handle,
							 1024 * 10,
							 NULL);

	bkwl_protocol_init_door_queue(app_queue_uart_door);
	while (1)
	{
		bprintf("twh bkwl_app_main num=%d\n\n", num);
		if (num > 0xffff)
			num = 0;
		num++;
		rtos_delay_milliseconds(1000);
	}

	bprintf("bkwl_app_main exit\n\n");
	return ret;
}

#endif
